#include "gps.h"

extern _SaveData Save_Data;

// 重定义fputc函数
int fputc(int ch, FILE *p)
{
    USART_SendData(USART2, (u8)ch);
    while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
        ; // 循环发送,直到发送完毕
    return ch;
}

void errorLog(int num)
{
    USART_SendString(USART2, "ERROR\r\n");
}

void parseGpsBuffer()
{
    char *subString;
    char *subStringNext;
    char i = 0;
    if (Save_Data.isGetData)
    {
        Save_Data.isGetData = false;
        USART_SendString(USART2, "**************\r\n");
        USART_SendString(USART2, Save_Data.GPS_Buffer);
        for (i = 0; i <= 6; i++)
        {
            if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == 0)
            {
                if (subString == NULL)
                    errorLog(1); // 解析错误
            }
            else
            {
                subString++;
                if ((subStringNext = strstr(subString, ",")) != NULL)
                {
                    char usefullBuffer[2];
                    switch (i)
                    {
                    case 1:
                        memcpy(Save_Data.UTCTime, subString, subStringNext - subString);
                        break; // 获取UTC时间
                    case 2:
                        memcpy(usefullBuffer, subString, subStringNext - subString);
                        break; // 获取UTC时间
                    case 3:
                        memcpy(Save_Data.latitude, subString, subStringNext - subString);
                        break; // 获取纬度信息
                    case 4:
                        memcpy(Save_Data.N_S, subString, subStringNext - subString);
                        break; // 获取N/S
                    case 5:
                        memcpy(Save_Data.longitude, subString, subStringNext - subString);
                        break; // 获取经度信息
                    case 6:
                        memcpy(Save_Data.E_W, subString, subStringNext - subString);
                        break; // 获取E/W

                    default:
                        break;
                    }

                    subString = subStringNext;
                    Save_Data.isParseData = true;
                    if (usefullBuffer[0] == 'A')
                        Save_Data.isUsefull = true;
                    else if (usefullBuffer[0] == 'V')
                        Save_Data.isUsefull = false;
                }
                else
                {
                    errorLog(2); // 解析错误
                }
            }
        }
    }
}

void printGpsBuffer()
{
    if (Save_Data.isParseData)
    {
        Save_Data.isParseData = false;

        USART_SendString(USART2, "Save_Data.UTCTime = ");
        USART_SendString(USART2, Save_Data.UTCTime);
        USART_SendString(USART2, "\r\n");

        if (Save_Data.isUsefull)
        {
            Save_Data.isUsefull = false;
            USART_SendString(USART2, "Save_Data.latitude = ");
            USART_SendString(USART2, Save_Data.latitude);
            USART_SendString(USART2, "\r\n");

            USART_SendString(USART2, "Save_Data.N_S = ");
            USART_SendString(USART2, Save_Data.N_S);
            USART_SendString(USART2, "\r\n");

            USART_SendString(USART2, "Save_Data.longitude = ");
            USART_SendString(USART2, Save_Data.longitude);
            USART_SendString(USART2, "\r\n");

            USART_SendString(USART2, "Save_Data.E_W = ");
            USART_SendString(USART2, Save_Data.E_W);
            USART_SendString(USART2, "\r\n");
        }
        else
        {
            USART_SendString(USART2, "GPS DATA is not usefull!\r\n");
        }
    }
}
